import 'package:ac/api/ac_urls.dart';
import 'package:ac/common/ac_pv_manager.dart';
import 'package:ac/common/style.dart';
import 'package:ac/data/ac_user_manager.dart';
import 'package:ac/model/ac_login_model.dart';
import 'package:ac/page/ac_message/ac_message_logic.dart';
import 'package:ac/page/ac_post_list/ac_post_list_logic.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class AcMoreUtil {
  late Userinfo userinfo;
  late VoidCallback onRefresh;
  late VoidCallback? blockCallback;

  AcMoreUtil(this.userinfo, this.onRefresh, {this.blockCallback});

  void show() {
    Get.bottomSheet(
      Container(
        color: const Color(0xfff1f1f1),
        width: 375.w,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            GestureDetector(
              onTap: () {
                if (userinfo.isFriend == true) {
                  unFollow(true);
                } else {
                  follow();
                }
                Get.back();
              },
              child: Container(
                height: 56.h,
                width: 375.w,
                color: Colors.white,
                alignment: Alignment.center,
                child: Text(
                  userinfo.isFriend == true ? 'Unfollow' : 'Follow',
                  style: TextStyle(
                    fontSize: 17.sp,
                    color: textColorBlack,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ),
            Container(
              height: 0.5.w,
              width: 375.w,
              color: const Color(0XFFBBBBBB),
            ),
            GestureDetector(
              onTap: () {
                if (userinfo.isBlock == true) {
                  unblock();
                } else {
                  block();
                }
                Get.back();
              },
              child: Container(
                height: 56.h,
                width: 375.w,
                color: Colors.white,
                alignment: Alignment.center,
                child: Text(
                  userinfo.isBlock == true ? 'Unblock' : 'Block',
                  style: TextStyle(
                    fontSize: 17.sp,
                    color: textColorBlack,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ),
            Container(
              height: 0.5.w,
              width: 375.w,
              color: const Color(0XFFBBBBBB),
            ),
            GestureDetector(
              onTap: () {
                Get.back();
                showReport();
              },
              child: Container(
                height: 56.h,
                width: 375.w,
                color: Colors.white,
                alignment: Alignment.center,
                child: Text(
                  'Report',
                  style: TextStyle(
                    fontSize: 17.sp,
                    color: textColorBlack,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 8.h,
            ),
            GestureDetector(
              onTap: () {
                Get.back();
              },
              child: Container(
                height: 56.h,
                width: 375.w,
                color: Colors.white,
                alignment: Alignment.center,
                child: Text(
                  'Cancel',
                  style: TextStyle(
                    fontSize: 17.sp,
                    color: textColorBlack,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void showReport() {
    Widget item(String title, {bool showBorder = true}) {
      return Column(
        children: [
          GestureDetector(
            onTap: () {
              report(title);
              Get.back();
            },
            child: Container(
              height: 56.h,
              width: 375.w,
              color: Colors.white,
              alignment: Alignment.center,
              child: Text(
                title,
                style: TextStyle(
                  fontSize: 17.sp,
                  color: textColorBlack,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ),
          ),
          if (showBorder)
            Container(
              height: 0.5.w,
              width: 375.w,
              color: const Color(0XFFBBBBBB),
            )
        ],
      );
    }

    Get.bottomSheet(
      Container(
        color: const Color(0xfff1f1f1),
        width: 375.w,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            item("Pornographic"),
            item("False gender"),
            item("Fraud"),
            item("Political sensitive"),
            item("Other",showBorder: false),
            SizedBox(
              height: 8.h,
            ),
            GestureDetector(
              onTap: () {
                Get.back();
              },
              child: Container(
                height: 56.h,
                width: 375.w,
                color: Colors.white,
                alignment: Alignment.center,
                child: Text(
                  'Cancel',
                  style: TextStyle(
                    fontSize: 17.sp,
                    color: textColorBlack,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> follow() async {
    LogPvManger.stat(page: LogPages.follow);
    var res = await AcUrls.follow.post(data: {"followUserId": userinfo.userId});
    if (res?["code"] == 0) {
      userinfo.isFriend = true;
      onRefresh();
      AcUserManager.updateBloggerUserInfo(userinfo);
      if (Get.isRegistered<AcPostListLogic>()) {
        Get.find<AcPostListLogic>().refreshUserInfo();
      }
      EasyLoading.showToast("Successful Follow");
      Get.find<AcMessageLogic>().initFollowList();
    }
  }

  Future<void> unFollow(bool showToast) async {
    LogPvManger.stat(page: LogPages.unFollow);
    var res =
        await AcUrls.unFollow.post(data: {"followUserId": userinfo.userId});
    if (res?["code"] == 0) {
      userinfo.isFriend = false;
      onRefresh();
      AcUserManager.updateBloggerUserInfo(userinfo);
      if (Get.isRegistered<AcPostListLogic>()) {
        Get.find<AcPostListLogic>().refreshUserInfo();
      }
      if (showToast) {
        EasyLoading.showToast("Successful Unfollow");
        Get.find<AcMessageLogic>().initFollowList();
      }
    }
  }

  Future<void> block() async {
    LogPvManger.stat(page: LogPages.blockPage);
    var res = await AcUrls.report.post(data: {
      "broadcasterId": userinfo.userId,
      "complainCategory": "Block",
    });
    if (res?["code"] == 0) {
      userinfo.isBlock = true;
      onRefresh();
      AcUserManager.updateBloggerUserInfo(userinfo);
      AcUserManager.updateBloggerUserInfo(userinfo);
      if (Get.isRegistered<AcPostListLogic>()) {
        Get.find<AcPostListLogic>().refreshUserInfo();
      }
      if (Get.isRegistered<AcMessageLogic>()) {
        Get.find<AcMessageLogic>().handleBlock(userinfo);
      }
      unFollow(false);
      blockCallback?.call();
      EasyLoading.showToast("Blocked successfully");
      Get.find<AcMessageLogic>().initFollowList();
    }
  }

  Future<void> unblock() async {
    LogPvManger.stat(page: LogPages.unBlockPage);
    var res = await AcUrls.unBlock.post(data: {
      "blockUserId": userinfo.userId,
    });
    if (res?["code"] == 0) {
      userinfo.isBlock = false;
      onRefresh();
      AcUserManager.updateBloggerUserInfo(userinfo);
      if (Get.isRegistered<AcPostListLogic>()) {
        Get.find<AcPostListLogic>().refreshUserInfo();
      }
      EasyLoading.showToast("Unblocked successfully");
    }
  }

  Future<void> report(String sub) async {
    LogPvManger.stat(page: LogPages.reportPage);
    var res = await AcUrls.report.post(data: {
      "broadcasterId": userinfo.userId,
      "complainCategory": "Report",
      "complainSub": sub,
    });
    if (res?["code"] == 0) {
      EasyLoading.showToast('Report success');
    }
  }
}
